var api = require('../../utils/api.js');
var app = getApp();
var cleartime;
Page({
    data: {

        top: 32,
        list: [],
        show:1,
        avatar:'1'

    },
    onLoad(ret) {
        var info = wx.getMenuButtonBoundingClientRect();
        var top = info.top;
        this.setData({
            top: top + 32
        })
    },

    getNickname(ret){
      console.log(ret);
      var user_nickname=ret.detail.value;
      var uid=this.data.user.id;
      var that=this;

      api.post({
        url: 'user/profile',
        data: {nickname:user_nickname},
        success: data => {
            that.setData({
                nickname: user_nickname,
              show:1
            })
        }
    })
  
  
  
    },

    onChooseAvatar(e) {

      var that=this;
      var avatar= e.detail.avatarUrl
      console.log(e);
      this.setData({
        avatar:avatar
      })
      var usrl = api.HOST;
      var uid=this.data.user.id;
      wx.uploadFile({
        filePath: avatar,
        name: 'file',
        url: usrl + '/api/user/setuseravatar?id='+uid,
        formData: {uid:uid},
        success: function (res) {
          var data = JSON.parse(res.data);
          console.log(data.data.img)
  
          that.setData({
            avatar: data.data.img
          });
          that.onShow();
  
        },
        fail: function (res) {
          console.log(res);
        }
      })
  
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        clearInterval(cleartime)
    },
    onUnload: function () { // 关闭清除定时器
        const self = this
        clearInterval(cleartime)
    },



    onShow() {
        var that = this;
        clearInterval(cleartime);
        // this.getmycase();
        let user = wx.getStorageSync('user');
        if (!user) {
            wx.navigateTo({
                url: '/pages/login/index'
            });
        }

        api.post({
            url: 'user/index',
            data: {},
            success: data => {
                var info = data.data;
                that.setData({
                    user: info,
                    avatar:info.avatar,
                    user_nickname:info.nickname
                })
                if(!info.avatar||!info.nickname){
                  that.setData({
                    show:0
                  })
                }
            }
        })
    },

    seeinfo() {
        wx.showToast({
            title: '手册',
        })
    },

    // 入库
    scanCodeEvent: function () {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true, // 只允许从相机扫码
            success(res) {
                JSON.stringify(res)
                var code = res.result;

                api.post({
                    url: 'order/setgoodsbycode',
                    data: {
                        code: code,
                        status: 1
                    },
                    success: data => {
                        var info = data.data;
                        wx.showToast({
                            title: data.msg,
                            icon: 'none'
                        })
                    }
                })
            }
        })
    },

    // 拍摄
    scanCodeEvents: function () {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true, // 只允许从相机扫码
            success(res) {
                JSON.stringify(res)
                console.log(res.result)
                wx.navigateTo({
                    url: '/pages/about/update?code=' + res.result,
                })
            }
        })
    },

    scanCodeEventover(){
        wx.navigateTo({
          url: '/pages/chuku/index',
        })
    },

    // 出库
    // scanCodeEventover() {
    //     var that = this;
    //     wx.scanCode({
    //         onlyFromCamera: true, // 只允许从相机扫码
    //         success(res) {
    //             JSON.stringify(res)
    //             var code = res.result;

    //             api.post({
    //                 url: 'wxapp/order/setgoodsbycode',
    //                 data: {
    //                     code: code,
    //                     status: 5
    //                 },
    //                 success: data => {
    //                     var info = data.data;
    //                     wx.showToast({
    //                         title: data.msg,
    //                         icon: 'none'
    //                     })
    //                 }
    //             })

    //         }
    //     })
    // },

    getmycase() {
        var that = this;
        api.post({
            url: 'wxapp/order/getmycase',
            data: {},
            success: data => {
                var info = data.data;


                cleartime = setInterval(function () {
                    var lists = [];
                    info.forEach(function (index) {
                        // console.log(index)
                        var djs = index.djs;
                        index.djs = djs - 1;
                        index.over = that.countTime(djs);
                        // console.log(index)
                        lists.push(index);
                    })

                    that.setData({
                        list: lists
                    })
                }, 1000)
            }
        })

    },

    countTime(leftTime) {
        let days, hours, minutes, seconds;

        if (leftTime >= 0) {


            hours = Math.floor(leftTime / 60 / 60);
            minutes = Math.floor(leftTime / 60 % 60);
            seconds = Math.floor(leftTime % 60);

            seconds = seconds < 10 ? "0" + seconds : seconds
            minutes = minutes < 10 ? "0" + minutes : minutes
            hours = hours < 10 ? "0" + hours : hours

            return hours + ':' + minutes + ':' + seconds
        } else {
            return '已截止'
        }
    },

    setuserinfo(){
        wx.navigateTo({
          url: '/pages/my/info',
        })
    },
    addproject(){
        wx.navigateTo({
          url: '/pages/car/quotaion',
        })
    },
    showmenu(){
        wx.switchTab({
          url: '/pages/list/index',
        })
    },

    nexturl(ret){
        console.log(ret);
        var url=ret.currentTarget.dataset.url;

        wx.navigateTo({
          url: url
        })

    },
    addpyq(){
        wx.navigateTo({
            url: "/pages/dongtai/index"
          })
    },
    seeoldinfo(){
        wx.reLaunch({
          url: '/pages/old/update',
        })
    }
});